<form lay-filter="initWorkForm" id="initWorkForm" class="layui-form open-form">
  <div class="open-model" id="initWorkFormContent">
    <div class="open-auto">
      <input type="hidden" name="productDetailId" class="layui-input" />
      <div class="layui-col-xs6">
        <div class="layui-form-item">
          <label class="layui-form-label" style="width: 100px">产品Sku名称</label>
          <div class="layui-input-block" style="margin-left: 130px">
            <input type="text" disabled name="skuName" class="layui-input" id="skuName" />
          </div>
        </div>
      </div>
      <div class="layui-col-xs6">
        <div class="layui-form-item">
          <label class="layui-form-label" style="width: 100px">产品SKU编号</label>
          <div class="layui-input-block" style="margin-left: 130px">
            <input type="text" disabled name="skuCode" class="layui-input" id="skuCode" />
          </div>
        </div>
      </div>
      <div class="layui-col-xs6">
        <div class="layui-form-item">
          <label class="layui-form-label" style="width: 100px">产品Sku规格号</label>
          <div class="layui-input-block" style="margin-left: 130px">
            <input type="text" disabled name="skuSpec" class="layui-input" id="skuSpec" />
          </div>
        </div>
      </div>
      <input type="hidden" name="warehouseTypeId" class="layui-input" />
      <input type="hidden" name="warehouseId" class="layui-input" id="warehouseId" />
      <input type="hidden" name="warehouseInventoryTypeId" class="layui-input" />
      <input type="hidden" name="palletBoxId" class="layui-input" />
      <input type="hidden" name="productTypeId" class="layui-input" />
      <input type="hidden" name="inTypeName" class="layui-input" />
      <input type="hidden" name="inType" class="layui-input" />
      <input type="hidden" name="purchaseSignForId" class="layui-input" />
      <input type="hidden" name="inventoryTypeCode" class="layui-input" />

      <div id="disabledDiv">
        <div class="layui-col-xs6">
          <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px">入库库区</label>
            <div class="layui-input-block" style="margin-left: 130px">
              <!-- <input type="hidden" name="warehouseAreaId" class="layui-input" /> -->
              <select name="warehouseAreaId" id="warehouseAreaIdSelect" lay-search="{caseSensitive:false, fuzzy: false}" lay-filter="warehouseAreaIdSelect"></select>
              <!-- <input type="text" disabled name="warehouseAreaName" class="layui-input" /> -->
            </div>
          </div>
        </div>
        <div class="layui-col-xs6">
          <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px">入库库位</label>
            <div class="layui-input-block" style="margin-left: 130px">
              <select name="warehouseShelfSpaceId" select-type="select" lay-search="{caseSensitive:false, fuzzy: false}" id="warehouseShelfSpaceIdSelect" lay-filter="warehouseShelfSpaceIdSelect"></select>
              <input type="text"  name="warehouseShelfSpaceCode" id="warehouseShelfSpaceCode" style="display: none"  class="layui-input" />
            </div>
          </div>
        </div>
      </div>
      <div class="layui-col-xs6">
        <div class="layui-form-item">
          <label class="layui-form-label" style="width: 100px">载具料箱</label>
          <div class="layui-input-block" style="margin-left: 130px">
            <select name="palletBoxId" select-type="select" lay-search="{caseSensitive:false, fuzzy: false}" id="palletBoxIdSelect" lay-filter="palletBoxIdSelect"></select>
            <button class="layui-btn layui-btn-xs" type="button" id="checkPalletBox" style="display: none">选择料箱</button>
          </div>
        </div>
      </div>
      <div class="layui-col-xs6 disableDiv" style="display: none">
        <div class="layui-form-item">
          <label class="layui-form-label" style="width: 100px">待入库数量</label>
          <div class="layui-input-block" style="margin-left: 130px">
            <input type="number" name="restQuantity" disabled class="layui-input" />
          </div>
        </div>
      </div>
      <div class="layui-col-xs6">
        <div class="layui-form-item">
          <label class="layui-form-label" style="width: 100px">入库数量</label>
          <div class="layui-input-block" style="margin-left: 130px">
            <input type="number" name="quantity" class="layui-input" id="quantity" />
          </div>
        </div>
      </div>

      <div class="layui-col-xs6">
        <div class="layui-form-item">
          <label class="layui-form-label" style="width: 100px">批次号</label>
          <div class="layui-input-block" style="margin-left: 130px">
            <input type="text"   name="batchNumber" class="layui-input" id="batchNumber" />
          </div>
        </div>
      </div>

      <div class="layui-col-xs6">
        <div class="layui-form-item">
          <label class="layui-form-label" style="width: 100px">入库单价（不含税）</label>
          <div class="layui-input-block" style="margin-left: 130px">
            <input type="text"   name="inDanPrice" class="layui-input" id="inDanPrice" />
          </div>
        </div>
      </div>

    </div>
    <div class="layui-form-item text-right">
      <button class="layui-btn" lay-filter="workFormAddSubmit" type="button" lay-submit>提交</button>
    </div>
    <!-- <table class="layui-table" lay-filter="initWorkTable" id="initWorkTable"></table> -->
  </div>
</form>
<script>
  layui.use(["layer", "form", "table", "admin", "laytpl"], async function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var config = layui.config;
    var admin = layui.admin;
    var upload = layui.upload;
    var laytpl = layui.laytpl;
    const nowData = { ...parentData };
    let data = nowData.data;
    console.log(nowData);
    
    data.quantity = "";
    if (data.purchaseSignForProductId) {
      $(".disableDiv").show();
    }
    await admin.initDataSelectPro(
      "enterprisecab/warehouse/admin/warehouseArea/list",
      {
        limit: 999,
        warehouseId: $("#warehouseId").val()
      },
      "warehouseAreaIdSelect",
      "仓库库区",
      async (d, obj) => {
        await admin.initDataSelectPro(
          "enterprisecab/warehouse/admin/warehouseShelfSpace/list",
          {
            limit: 999,
            queryWarehouseAreaId: d.value,
            queryWarehouseId: $("#warehouseId").val(),
            status: 1
          },
          "warehouseShelfSpaceIdSelect",
          "库区库位",
          async (d, obj) => {
           $("#warehouseShelfSpaceCode").val(obj.code);

          const res= await admin.initDataSelectPro(
              "enterprisecab/warehouse/admin/palletBoxList/getBySpaceCode",
              {
                spaceCode: obj.code
              },
              "palletBoxIdSelect",
              "载具料箱",
               async(d, obj) => {
               },
              "code"
            );


              if(res!=undefined&&res.length>0){
                $("#checkPalletBox").hide();
              }else{
                $("#checkPalletBox").show();
              }
          },
          "code"
        );
      }
    );
    if (data.warehouseAreaId) {
      console.log(111111, data.warehouseAreaId);

      admin.setFormDisable("#disabledDiv", true);
      const res = await admin.initDataSelectPro(
        "enterprisecab/warehouse/admin/warehouseShelfSpace/list",
        {
          limit: 999,
          queryWarehouseAreaId: data.warehouseAreaId,
          queryWarehouseId: $("#warehouseId").val(),
          status: 1
        },
        "warehouseShelfSpaceIdSelect",
        "库区库位",
        async (d, obj) => {
          let res = await admin.reqPro("enterprisecab/warehouse/admin/palletBoxList/getBySpaceCode", { spaceCode: d.value });
        },
        "code"
      );
      console.log(22, res);

      await admin.initDataSelectPro(
        "enterprisecab/warehouse/admin/palletBoxList/getBySpaceCode",
        {
          spaceCode: nowData.data.warehouseShelfSpaceCode
        },
        "palletBoxIdSelect",
        "载具料箱",
        async (d, obj) => {},
        "code"
      );
    }
    console.log("data", data);

    form.val("initWorkForm", data);

    form.on("submit(workFormAddSubmit)", function (data) {
      const formData = data.field;
      console.log(formData);
      
    // return
      // return
      // formData.palletBoxId = formData.palletBoxId ? formData.palletBoxId : "";
      layer.load(2);
      // return;
      if (nowData.eventName == "add") {
        formData.purchaseStorageWorkProduct = {
          ...nowData.data,
          ...formData,
        };
        // formData.purchaseSignForId=nowData.data.signNo
        admin.req(
          "enterprise/purchase/admin/purchaseStorageWork/add",
          JSON.stringify(formData),
          function (res) {
            layer.closeAll("loading");
            if (res.code == 200) {
              layer.msg(res.msg, {
                icon: 1
              });
              table.reload("locationTable");
              // layer.closeAll("page");
              layer.close(nowData.zindex);
              childData.status = "success";
              childData.quantity = formData.quantity;
            } else {
              layer.msg(res.msg, {
                icon: 2
              });
            }
          },
          "POST",
          false,
          "application/json"
        );
      } else if (nowData.eventName == "simpleAdd") {
        admin.req(
          "enterprise/purchase/admin/purchaseStorageWork/simpleStorage",
          JSON.stringify(formData),
          function (res) {
            layer.closeAll("loading");
            console.log(nowData);

            if (res.code == 200) {
              layer.msg(res.msg, {
                icon: 1
              });
              table.reload("locationTable");
              layer.close(nowData.zindex);
            }else {
              layer.msg(res.msg, {
                icon: 2
              });
            }
          },
          "POST",
          false,
          "application/json"
        );
      }
    });

    $("#checkPalletBox").click(function () {

      admin.formOpen({
        area: ["90%", "85%"],
        title: "料箱清单",
        path: "components/warehouse/palletBoxList.html",
        success: function () {
          parentData.eventName = "radio";
          parentData.warehouseShelfSpaceIdSelect = $("#warehouseShelfSpaceIdSelect").val();
          parentData.warehouseShelfSpaceCode = $("#warehouseShelfSpaceCode").val();

          parentData.where = {
              spaceCodeFlag: "doWorkFlag",
              page:1,
              limit:10
          };
        },
        end: function () {
          let obj = childData.formObjOut;
          if(obj!=undefined){
            admin.initDataSelectPro(
                    "enterprisecab/warehouse/admin/palletBoxList/getBySpaceCode",
                    {
                      spaceCode: obj.spaceCode
                    },
                    "palletBoxIdSelect",
                    "载具料箱",
                    async(d, obj) => {
                    },
                    "code"
            );
          }

        }
      });
    });



  });
</script>
